#include<stdio.h> 
#include<bits/stdc++.h> 
using namespace std;
int n,m=0,t[1005];
string s[1005];
int zhuang(string z){
	stringstream z0;
	int z1=0;
	z0.clear();
	z0<<z;
	z0>>z1;
	return z1;
}
int ok(string z){
	int a[5],e,f0,f1,b=0,c=0;
	string z1;
	for(int i=1;i<=m;i++){
		if(s[i]==z)return t[i];
	}
	//---------------------------------
	for(int i=0;i<z.size();i++) {
		if(z[i]=='.')b++;
		if(z[i]==':'&&b!=3)return -1;
		if(z[i]==':'&&b==3)c++;
	}
	if(b!=3||c!=1)return -1;
	//--------------------------------- 
	f0=z.find(".");
	z1=z.substr(0,f0);
	if(z1.size()>1&&z1[0]=='0'||z1.size()>3||z1.empty())return -1;
	a[1]=zhuang(z1);
	if(a[1]>255)return -1;
	//--------------------------
	for(int i=2;i<=4;i++){
		if(i==4)f1=z.find(":");
		else f1=z.find(".",f0+1);
		
		z1=z.substr(f0+1,f1-f0-1);
		if(z1.size()>1&&z1[0]=='0'||z1.size()>3||z1.empty())return -1;
		a[i]=zhuang(z1);
		if(a[i]>255)return -1;
		f0=f1;
	}
	//--------------------------
	z1=z.substr(f0+1);
	if(z1.size()>1&&z1[0]=='0'||z1.size()>5||z1.empty())return -1;
	e=zhuang(z1);
	if(e>65535)return -1;
	//-------------------------------
	return 0;
}
int main(){
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++){
		string op,ad;
		cin>>op;
		if(op=="Server"){
			cin>>ad;
			int j=ok(ad);
			if(ok(ad)==0){
				cout<<"OK\n";
				m++;
				s[m]=ad;
				t[m]=i;
			}else {
				if(j==-1)cout<<"ERR\n";
				else cout<<"FAIL\n";
			}
		}else{
			cin>>ad;
			int j=ok(ad);
			if(j>0){
				cout<<j<<endl;
			}else{
				if(j==-1)cout<<"ERR\n";
				else cout<<"FAIL\n";
			}
		}
	}
	return 0;
}
